package filters;

import interfaces.IMap;



public class SobelFilter implements IFilter
{
	float FMapH[][];
	float FMapHR[][];
	float FMapV[][];
	float FMapVR[][];
	
	float D1[][];
	float D2[][];
	float D3[][];
	float D4[][];
    //===========================================================================
    public SobelFilter()
    {
        FMapH = new float[3] [3];
        FMapH[0] [0] = -1;
        FMapH[1] [0] = 0;
        FMapH[2] [0] = 1;

        FMapH[0] [1] = -2;
        FMapH[1] [1] = 0;
        FMapH[2] [1] = 2;

        FMapH[0] [2] = -1;
        FMapH[1] [2] = 0;
        FMapH[2] [2] = 1;
        
        FMapHR = new float[3] [3];
        FMapHR[0][ 0] = 1;
        FMapHR[1][ 0] = 0;
        FMapHR[2][ 0] = -1;

        FMapHR[0][ 1] = 2;
        FMapHR[1][ 1] = 0;
        FMapHR[2][ 1] = -2;

        FMapHR[0][ 2] = 1;
        FMapHR[1][ 2] = 0;
        FMapHR[2][ 2] = -1;
        
        FMapV = new float[3] [3];
        FMapV[0][0] = -1;
        FMapV[0][1] = 0;
        FMapV[0][2] = 1;

        FMapV[1][0] = -2;
        FMapV[1][ 1] = 0;
        FMapV[1][ 2] = 2;

        FMapV[2][ 0] = -1;
        FMapV[2][ 1] = 0;
        FMapV[2][ 2] = 1;
        
        FMapVR = new float[3] [3];
        FMapVR[0][ 0] = 1;
        FMapVR[0][ 1] = 0;
        FMapVR[0][ 2] = -1;

        FMapVR[1][0] = 2;
        FMapVR[1][1] = 0;
        FMapVR[1][ 2] = -2;

        FMapVR[2][ 0] = 1;
        FMapVR[2][1] = 0;
        FMapVR[2][ 2] = -1;
        
        
        D1 = new float[3] [3];
        D1[0] [0] = 2;
        D1[1] [0] = 0;
        D1[2] [0] = 0;

        D1[0] [1] = 0;
        D1[1] [1] = 0;
        D1[2] [1] = 0;

        D1[0] [2] = 0;
        D1[1] [2] = 0;
        D1[2] [2] = -2;
        
        D2 = new float[3] [3];
        D2[0] [0] = -2;
        D2[1] [0] = 0;
        D2[2] [0] = 0;

        D2[0] [1] = 0;
        D2[1] [1] = 0;
        D2[2] [1] = 0;

        D2[0] [2] = 0;
        D2[1] [2] = 0;
        D2[2] [2] = 2;
        
        D3 = new float[3] [3];
        D3[0] [0] = 0;
        D3[1] [0] = 0;
        D3[2] [0] = 2;

        D3[0] [1] = 0;
        D3[1] [1] = 0;
        D3[2] [1] = 0;

        D3[0] [2] = -2;
        D3[1] [2] = 0;
        D3[2] [2] = 0;
        
        D4 = new float[3] [3];
        D4[0] [0] = 0;
        D4[1] [0] = 0;
        D4[2] [0] = -2;

        D4[0] [1] = 0;
        D4[1] [1] = 0;
        D4[2] [1] = 0;

        D4[0] [2] = 2;
        D4[1] [2] = 0;
        D4[2] [2] = 0;

    }
    //===========================================================================
    public int ApplyFilter(int Px,int Py, IMap map)
    {
    	int sum=0;
        int sumH=0;
        int sumHR=0;
        int sumV=0;
        int sumVR=0;
        
        int sumD1=0;
        int sumD2=0;
        int sumD3=0;
        int sumD4=0;
        for (int y = -1; y < 2; y++)
            for (int x = -1; x < 2; x++)
            {
            	int xf=x + 1;
            	int yf=y + 1;
            	float C = map.Get(Px + x,Py + y);
            	sumH += (int)(C * FMapH[xf] [yf]);
            	sumHR +=(int)(C * FMapHR[xf] [yf]);
            	sumV += (int)(C * FMapV[xf] [yf]);
            	sumVR +=(int)(C * FMapVR[xf] [yf]);
            	
            	sumD1 +=(int)(C * D1[xf] [yf]);
            	sumD2 +=(int)(C * D2[xf] [yf]);
            	sumD3 +=(int)(C * D3[xf] [yf]);
            	sumD4 +=(int)(C * D4[xf] [yf]);
            }
        sum=0;
        int vector=0;
        if(sumH>sum)
        {
        	sum=sumH;
        	vector =1;
        }
        if(sumHR>sum)
        {
        	sum=sumHR;
        	vector =1;
        }
        if(sumV>sum)
        {
        	sum=sumV;
        	vector =2;
        }
        if(sumVR>sum)
        {
        	sum=sumVR;
        	vector =2;
        }
        
        if(sumD1>sum)
        {
        	sum=sumD1;
        	vector =3;
        }
        if(sumD2>sum)
        {
        	sum=sumD2;
        	vector =3;
        }
        if(sumD3>sum)
        {
        	sum=sumD3;
        	vector =4;
        }
        if(sumD4>sum)
        {
        	sum=sumD4;
        	vector =4;
        }
        
        if (sum > 255)
            return (int)255;
        if (sum < 0)
            return (int)0;
        return (int)sum + (vector<<24);
    }
    //===========================================================================

}
